﻿using Quartz;

namespace Share;

public class SampleJob : IJob
{   
    public static readonly JobKey Key = JobKey.Create(nameof(SampleJob), "group");


    // 如果 Job 里定义了属性，同时 JobDataMap 中有相同的名字，会自动匹配，不需要以下语句获取JobData
    //string greeting = context.JobDetail.JobDataMap.GetString("greeting");
    //string user = context.JobDetail.JobDataMap.GetString("user");
    // jobdetail 和 trigger 都有 jobdata，同名 trigger 覆盖 jobdetail
    // 所以尽量不要用以上语句获取 jobdata，改用以下语句
    //var goodMethod = context.MergedJobDataMap.GetString("a-value");

    public string Greeting { get; set; } = string.Empty;
    public string User { get; set; } = string.Empty;


    public async Task Execute(IJobExecutionContext context)
    {
        if (context.RefireCount > 10)
        {
            // we might not ever succeed!
            // maybe log a warning, throw another type of error, inform the engineer on call
            return;
        }

        try
        {
            //string greeting = context.JobDetail.JobDataMap.GetString("greeting");
            //string user = context.JobDetail.JobDataMap.GetString("user");
            Console.WriteLine($"{Greeting} {User}");

            // ... do work
            await Task.Delay(100);
        }
        catch (Exception ex)
        {
            // do you want the job to refire?
            throw new JobExecutionException(msg: "", refireImmediately: true, cause: ex);
        }
    }
}
